<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

global $dal;
aos_header( 'Trade' );
?>
					<table id="values">
<?php
$result2 = mysql_query( "SELECT coal_gold, iron_gold, food_gold, turn " .
		"FROM game" ) or die ( "Query failed: " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$current_turn = $row2[ 'turn' ];
$gold_iron = 1 / $row2[ 'iron_gold' ];
$gold_coal = 1 / $row2[ 'coal_gold' ];
$gold_food = 1 / $row2[ 'food_gold' ];
$iron_coal = $row2[ 'iron_gold' ] / $row2[ 'coal_gold' ];
$coal_iron = 1 / $iron_coal;
$food_coal = $row2[ 'food_gold' ] / $row2[ 'coal_gold' ];
$coal_food = 1 / $food_coal;
$iron_food = $row2[ 'iron_gold' ] / $row2[ 'food_gold' ];
$food_iron = 1 / $iron_food;
?>
						<tr>
							<td width="25%">&nbsp;</td>
							<td align="right">
								<strong class="gold"><?php transprint( 'Gold' ); ?></strong>
							</td>
							<td align="right">
								<strong class="iron"><?php transprint( 'Iron' ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal"><?php transprint( 'Coal' ); ?></strong>
							</td>
							<td align="right">
								<strong class="food"><?php transprint( 'Food' ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="gold"><?php transprint( 'Gold' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold">1</strong>
							</td>
							<td align="right">
								<strong class="iron"><?php print( round( $gold_iron, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal"><?php print( round( $gold_coal, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="food"><?php print( round( $gold_food, 2 ) ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="iron"><?php transprint( 'Iron' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold"><?php print( round( $row2[ 'iron_gold' ], 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong id="iron">1</strong>
							</td>
							<td align="right">
								<strong class="coal"><?php print( round( $iron_coal, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="food"><?php print( round( $iron_food, 2 ) ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="coal"><?php transprint( 'Coal' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold"><?php print( round( $row2[ 'coal_gold' ], 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="iron"><?php print( round( $coal_iron, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal">1</strong>
							</td>
							<td align="right">
								<strong class="food"><?php print( round( $coal_food, 2 ) ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="food"><?php transprint( 'Food' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold"><?php print( round( $row2[ 'food_gold' ], 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="iron"><?php print( round( $food_iron, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal"><?php print( round( $food_coal, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="food">1</strong>
							</td>
						</tr>
					</table>
<?php
include( '../global/resources.php' );
?>
					<div class="clear"></div>
					<br />
					<form method="post" action="update_auto_trades.php">
						<?php printAutoTradeTable( 'gold', true ); ?>
						<?php printAutoTradeTable( 'gold', false ); ?>
						<?php printAutoTradeTable( 'iron', true ); ?>
						<?php printAutoTradeTable( 'iron', false ); ?>
						<?php printAutoTradeTable( 'coal', true ); ?>
						<?php printAutoTradeTable( 'coal', false ); ?>
						<?php printAutoTradeTable( 'food', true ); ?>
						<?php printAutoTradeTable( 'food', false ); ?>
						<input type="submit" value="<?php transprint( 'confirm' ); ?>" />
					</form>
<?php
aos_footer();

function printAutoTradeTable( $for, $below ) {
	global $dal;
	
	$prefix = $for . '_' . ( $below ? 'below' : 'above' ) . '_';
	
	if( $below ) {
		$belowValue = 1; 
		$statement = $dal->prepare( 'SELECT * ' .
				'FROM auto_trade ' .
				'WHERE ruler_id = :ruler_id AND resource_requested = :resource_requested AND below = :below' );
		$statement->bindParam( ':resource_requested', $for, PDO::PARAM_STR );
	}
	else {
		$belowValue = 0; 
		$statement = $dal->prepare( 'SELECT * ' .
				'FROM auto_trade ' .
				'WHERE ruler_id = :ruler_id AND resource_offered = :resource_offered AND below = :below' );
		$statement->bindParam( ':resource_offered', $for, PDO::PARAM_STR );
	}
	$statement->bindParam( ':below', $belowValue, PDO::PARAM_INT );
	$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$previous = $dal->getObject( $statement );
	if( isset( $previous ) ) {
		$previous->against = ( $for == $previous->resource_offered ) ? $previous->resource_requested : $previous->resource_offered; 
	}
	else {
		// Set default values
		$previous = new StdClass();
		$previous->enabled = 0;
		$previous->against = '';
		$previous->trade_below = 10000;
		$previous->trade_over = 100000;
		$previous->amount = 1000;
		$previous->pricing = 2;
	}
?>	
						<table>
							<tr>
								<th colspan="5"><?php transprint( $below ? 'Automatically request' : 'Automatically offer' ); ?> <strong class="<?php print( $for ); ?>"><?php transprint( $for ); ?></strong></th>
							</tr>
							<tr>
								<td><?php print( $previous->enabled == 1 ? '<strong>' . translate( 'Enabled' ) . '</strong>' : translate( 'Disabled' ) ); ?></td>
								<td><?php transprint( 'When' ); ?> <strong class="<?php print( $for ); ?>"><?php transprint( $for ); ?></strong> <?php transprint( $below ? 'is below' : 'is above' ); ?></td>
								<td><?php transprint( 'Trade amount' ); ?></td>
								<td><?php transprint( $below ? 'Offer' : 'Request' ); ?></td>
								<td><?php transprint( 'Pricing' ); ?></td>
							</tr>
							<tr class="even">
								<td>
									<select name="<?php print( $prefix ); ?>enable">
										<option value="0"<?php print( $previous->enabled == 0 ? ' selected="selected"' : '' ); ?>><?php transprint( 'disabled' ); ?></option>
										<option value="1"<?php print( $previous->enabled == 1 ? ' selected="selected"' : '' ); ?>><?php transprint( 'enabled' ); ?></option>
									</select>
								</td>
								<td>
<?php
	if( $below ) {
?>
									<input type="text" size="5" name="<?php print( $prefix ); ?>below" value="<?php print( $previous->trade_below ); ?>" />
<?php
	}
	else {
?>
									<input type="text" size="5" name="<?php print( $prefix ); ?>above" value="<?php print( $previous->trade_over ); ?>" />
<?php
	}
?>
								</td>
								<td>
									<input type="text" size="5" name="<?php print( $prefix ); ?>amount" value="<?php print( $previous->amount ); ?>" />
								</td>
								<td>
									<select name="<?php print( $prefix ); ?>against">
<?php
	if( $for != 'gold' ) {
?>
										<option value="gold"<?php print( $previous->against == 'gold' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Gold' ); ?></option>
<?php
	}
	if( $for != 'food' ) {
?>
										<option value="food"<?php print( $previous->against == 'food' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Food' ); ?></option>
<?php
	}
	if( $for != 'coal' ) {
?>
										<option value="coal"<?php print( $previous->against == 'coal' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Coal' ); ?></option>
<?php
	}
	if( $for != 'iron' ) {
?>
										<option value="iron"<?php print( $previous->against == 'iron' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Iron' ); ?></option>
<?php
	}
?>
									</select>
								</td>
								<td>
									<select name="<?php print( $prefix ); ?>pricing">
										<option value="0"<?php print( $previous->pricing == 0 ? ' selected="selected"' : '' ); ?>><?php transprint( 'very expensive' ); ?></option>
										<option value="1"<?php print( $previous->pricing == 1 ? ' selected="selected"' : '' ); ?>><?php transprint( 'expensive' ); ?></option>
										<option value="2"<?php print( $previous->pricing == 2 ? ' selected="selected"' : '' ); ?>><?php transprint( 'fair' ); ?></option>
										<option value="3"<?php print( $previous->pricing == 3 ? ' selected="selected"' : '' ); ?>><?php transprint( 'cheap' ); ?></option>
										<option value="4"<?php print( $previous->pricing == 4 ? ' selected="selected"' : '' ); ?>><?php transprint( 'very cheap' ); ?></option>
									</select>
								</td>
							</tr>
						</table>
						<br />
<?php	
}
?>